"""
create : 2012-02-10
author: wangxing <wangxing.pku@gmail.com>
desp : this file is used for get regression result from weka result file
"""

def getRegResultSum(fname):
    """
    get regression result for summary regress

    hint str is 
=== Error on test data ===
Correlation coefficient                  0.7575
Mean absolute error                      0.6688
Root mean squared error                  0.835 
Relative absolute error                 62.7283 %
Root relative squared error             64.5075 %
    """
    lines = open(fname).readlines()
    find = False
    rindex = 0
    for i in range(len(lines)):
        if lines[i].startswith("=== Error on test data ==="):
            find = True
            rindex = i+1
            break
    
    #
    tmplines = []
    for i in range(rindex,len(lines)):
        if lines[i].strip() != "":
            tmplines.append(lines[i])
    
    #here do sth 
    rdic = {}
    #cf
    rdic['CF'] = float(splitline(tmplines[0]," ")[2])
    rdic['MAE'] = float(splitline(tmplines[1]," ")[3])
    rdic['RMSE'] = float(splitline(tmplines[2]," ")[4])
    rdic['RAE'] = float(splitline(tmplines[3]," ")[3])
    rdic['RRSE'] = float(splitline(tmplines[4]," ")[4])

    return rdic

def splitline(line,spliter):
    line = line.strip()
    ws = line.split(spliter)
    r = []
    for w in ws:
        if w != "":
            r.append(w)
    return r
